package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TNucleateResidentTask;
import com.ruoyi.system.domain.TReleaseTask;
import com.ruoyi.system.domain.TResident;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.TNucleateResidentTaskMapper;
import com.ruoyi.system.mapper.TResidentMapper;
import com.ruoyi.system.utils.AESUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.token.Token;
import org.springframework.security.core.token.TokenService;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TLoginMapper;
import com.ruoyi.system.domain.TLogin;
import com.ruoyi.system.service.ITLoginService;

import javax.servlet.http.HttpServletRequest;

/**
 * 注册Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-17
 */
@Service
public class TLoginServiceImpl implements ITLoginService 
{
    @Autowired
    private TLoginMapper tLoginMapper;

    @Autowired
    private TResidentMapper residentMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TNucleateResidentTaskMapper nucleateResidentTaskMapper;


    /**
     * 查询注册
     * 
     * @param id 注册主键
     * @return 注册
     */
    @Override
    public TLogin selectTLoginById(Long id)
    {
        return tLoginMapper.selectTLoginById(id);
    }

    /**
     * 查询注册列表
     * 
     * @param tLogin 注册
     * @return 注册
     */
    @Override
    public List<TLogin> selectTLoginList(TLogin tLogin)
    {
        return tLoginMapper.selectTLoginList(tLogin);
    }

    /**
     * 新增注册
     * 
     * @param tLogin 注册
     * @return 结果
     */
    @Override
    public int insertTLogin(TLogin tLogin)
    {
        return tLoginMapper.insertTLogin(tLogin);
    }

    /**
     * 修改注册
     * 
     * @param tLogin 注册
     * @return 结果
     */
    @Override
    public int updateTLogin(TLogin tLogin)
    {
        return tLoginMapper.updateTLogin(tLogin);
    }

    /**
     * 批量删除注册
     * 
     * @param ids 需要删除的注册主键
     * @return 结果
     */
    @Override
    public int deleteTLoginByIds(Long[] ids)
    {
        return tLoginMapper.deleteTLoginByIds(ids);
    }

    /**
     * 删除注册信息
     * 
     * @param id 注册主键
     * @return 结果
     */
    @Override
    public int deleteTLoginById(Long id)
    {
        return tLoginMapper.deleteTLoginById(id);
    }

    // 注册
    @Override
    public AjaxResult add(LoginBody loginBody) {
        // 创建注册
        TLogin tLogin = new TLogin();
        tLogin.setSex(loginBody.getSex().toString());
        tLogin.setPhone(loginBody.getPhone().toString());
        tLogin.setCommunityId(loginBody.getCommunityId());
        tLogin.setBuildingId(loginBody.getBuildingId());
        BeanUtils.copyProperties(loginBody,tLogin);
        // 创建注册信息
        TResident tResident = new TResident();
        tResident.setState("1");
        tResident.setResident(loginBody.getUsername());
        tResident.setPhone(loginBody.getPhone().longValue());
        tResident.setAddress(loginBody.getAddress());
        tResident.setCommunityId(loginBody.getCommunityId());
        tResident.setBuildingId(loginBody.getBuildingId());
        BeanUtils.copyProperties(loginBody,tLogin);
        residentMapper.insertTResident(tResident);
        tLogin.setResidentId(tResident.getId());
//        System.err.println(s);
        tLogin.setPassword(SecurityUtils.encryptPassword("123456"));

        // 创建系统
        SysUser sysUser = new SysUser();
        sysUser.setUserName(tLogin.getUsername());
        sysUser.setNickName(tLogin.getUsername());
        sysUser.setPhonenumber(tLogin.getPhone());

        TReleaseTask tReleaseTask = new TReleaseTask();



        // 对密码进行加密
        sysUser.setPassword(tLogin.getPassword());
        sysUserMapper.insertUser(sysUser);
        tLogin.setUserId(sysUser.getUserId());
        tLoginMapper.insertTLogin(tLogin);
        return AjaxResult.success();
    }



//    // 二维码
//    @Override
//    public String qrcode(HttpServletRequest request) {
//        Token token = tokenService.allocateToken(String.valueOf(request));
//
//        TLogin tLogin = tLoginMapper.selectTLoginById(token.getKeyCreationTime());
//
//        TNucleateResidentTask tNucleateResidentTask = nucleateResidentTaskMapper.selectTNucleateResidentTaskById(tLogin.getResidentId());
//
//
//
//        return null;
//    }
}
